<?php

namespace app\system\controller\finance;

use app\common\controller\System;
use app\system\controller\goods\ZipArchive;
use ba\Filesystem;
use hehe\Numeral;
use hehe\Str;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\Db;

/**
 *
 */
class Detail extends System {
    /**
     * Goods模型对象
     * @var object
     * @phpstan-var \app\admin\model\goods\Goods
     */
    protected object $model;

    protected string|array $defaultSortField = 'id,desc';

    protected array|string $preExcludeFields = ['id', 'create_time'];

    protected string|array $quickSearchField = [];

    public function initialize(): void {
        parent::initialize();
        $this->model = new \app\system\model\finance\Detail;
    }




    public function index(): void {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->param('select')) {
            $this->select();
        }
        list($where, $alias, $limit, $order) = $this->queryBuilder();

        $param = $this->request->param();

        $account = Db::name('finance_account')->where(['id' => $param['account_id'], 'filter_id' => $this->filter['id']])->find();
        if(!$account) $this->error('账户不存在');


        if(empty($param['month'])){
            $res = $this->model->where($where)->where(['account_id' => $param['account_id']])->whereYear('trade_time', $param['year']);
        }else{
            $month = $param['month'] > 9 ? $param['month'] : '0' . $param['month'];
            $res = $this->model->where($where)->where(['account_id' => $param['account_id']])->whereMonth('trade_time', $param['year'] . '-' . $month);
        }

        if(!empty($param['pay'])){
            if($param['pay'] == '入金'){
                $res = $res->whereNotNull('income')->where('income', '>', 0);
            }
        }
        if(!empty($param['pay'])){
            if($param['pay'] == '出金'){
                $res = $res->whereNotNull('expenditure')->where('expenditure', '>', 0);
            }
        }

        $res = $res->order('trade_time desc, id desc')->paginate($limit);
        $list = $res->items();

        foreach($list as $key => $val){

            $list[$key]['trade_time'] = date('Y-m-d H:i:s', $val['trade_time']);
            $list[$key]['amount'] = Numeral::numberFormat($val['amount'] / 100);
            $list[$key]['income'] = Numeral::numberFormat($val['income'] / 100);
            $list[$key]['expenditure'] = Numeral::numberFormat($val['expenditure'] / 100);
            $list[$key]['desc'] = empty($val['desc']) ? null : html_entity_decode($val['desc']);
            if(empty($val['invoice'])){
                $list[$key]['invoice'] = false;
            }else{
                $invoice = json_decode($val['invoice'], true);
                $invoice['name'] = html_entity_decode($invoice['name']);
                $list[$key]['invoice'] = $invoice;
            }
        }




        $this->success('', [
            'list' => $res->items(),
            'total' => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }




}